<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
// import resize from "./mixins/resize";
export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
    addressPortraitList: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {},
  computed: {
    newNameData() {
      return this.addressPortraitList.map((item) => item.name);
    },
    countArray() {
      return this.addressPortraitList.map((item) => item.count);
    },
  },
  watch: {
    // 监听 newNameData 和 countArray 的变化
    newNameData(newVal, oldVal) {
      if (newVal.length > 0) {
        this.initChart();
      }
    },
    countArray(newVal, oldVal) {
      if (newVal.length > 0) {
        this.initChart();
      }
    },
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      const that = this;
      this.chart.setOption({
        title: {
          text: "意向地区分布",
          left: "",
          textStyle: {
            color: "#333", // 字体颜色设置为黑色
            fontWeight: "bold", // 加粗字体
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        xAxis: {
          type: "category",
          data: 
            // "北仑",
            // "新碶",
            // "宁波",
            // "大碶",
            // "小港",
            // "浙江",
            // "春晓",
            // "东钱湖",
            // "鄞州",
            // "镇海",
            // "江北",
            // "象山",
            // "海曙",
            // "奉化",
            // "余姚",
            // "慈溪",
            // "江北区",
            // "上海",
            // "威海",
            // "江东区",
            // "镇海区",
            // "奉化市",
            // "颐景园",
            this.newNameData,
          axisLabel: {
            interval: 0,
            rotate: 45,
            color: "#333",
          },
        },
        yAxis: {
          type: "value",
          max: 700,
          textStyle: {
            color: "#333",
          },
          axisLabel: {
            color: "#333",
          },
        },
        series: [
          {
            data:
              // 586, 246, 210, 125, 68, 31, 19, 15, 13, 11, 9, 8, 7, 6, 5, 4, 3,
              // 3, 3, 2, 2, 1, 1,
              this.countArray,
            type: "bar",
            itemStyle: {
              color: "#5470c6",
            },
          },
        ],
        dataZoom: [
          {
            type: "inside",
            start: 0,
            end: 100,
          },
          {
            type: "slider",
            start: 0,
            end: 100,
          },
        ],
      });
    },
  },
};
</script>

<style scoped></style>
